package com.cruise.game.service.impl.game;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cruise.game.mapper.GameCategoryMapper;
import com.cruise.game.model.entity.GameCategory;
import com.cruise.game.model.vo.admin.GameCategoryVO;
import com.cruise.game.service.game.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 游戏分类服务实现
 *
 * @author cruise
 * @date 2025-02-28
 */
@Slf4j
@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    private GameCategoryMapper gameCategoryMapper;

    @Override
    public List<GameCategoryVO> getAllCategories() {
        // 查询所有启用状态的分类
        List<GameCategory> categories = gameCategoryMapper.selectList(
                new LambdaQueryWrapper<GameCategory>()
                        .eq(GameCategory::getStatus, "active")
                        .orderByAsc(GameCategory::getId)
        );

        // 转换为VO
        return categories.stream()
                .map(this::convertToGameCategoryVO)
                .collect(Collectors.toList());
    }

    @Override
    public GameCategoryVO getCategoryById(Long categoryId) {
        GameCategory category = gameCategoryMapper.selectById(categoryId);

        if (category == null || !"active".equals(category.getStatus())) {
            return null;
        }

        return convertToGameCategoryVO(category);
    }

    /**
     * 转换为游戏分类VO
     */
    private GameCategoryVO convertToGameCategoryVO(GameCategory category) {
        GameCategoryVO vo = new GameCategoryVO();
        BeanUtils.copyProperties(category, vo);
        return vo;
    }
}